package com.sxm.yiwei.adapters;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.sxm.yiwei.R;
import com.sxm.yiwei.activitis.HomeSearchActivity;
import com.sxm.yiwei.beans.ShopContent;
import com.sxm.yiwei.utils.InternetAddressUtis;

import org.xutils.x;

import java.util.List;

/**
 * Created by KuKa on 2017/7/7.
 * 搜索列表
 */

public class SearchListviewAdapter extends BaseAdapter {
    Context context;
   public List<ShopContent.DataBean.ServiceListBean> shopList;
    int TYPE;
    public SearchListviewAdapter(Context context,  List<ShopContent.DataBean.ServiceListBean> shopList,int TYPE) {
        this.context=context;
        this.shopList=shopList;
        this.TYPE=TYPE;
    }

    @Override
    public int getCount() {
        return shopList.size();
    }

    @Override
    public Object getItem(int position) {
        return shopList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = null;
        ViewHolder holder;
        if (convertView==null){
            holder = new ViewHolder();
            if (TYPE==0){
                view= View.inflate(context, R.layout.search_listitem,null);
            }else {
                view= View.inflate(context, R.layout.search_listitem,null);
            }

            holder.head = (ImageView) view.findViewById(R.id.search_listimg);
            holder.contenttext = (TextView) view.findViewById(R.id.search_contenttext);
            holder.listdistance = (TextView) view.findViewById(R.id.search_listdistance);
            holder.listname = (TextView) view.findViewById(R.id.search_listname);
            holder.listpeople = (TextView) view.findViewById(R.id.search_listpeople);
            holder.pinglun = (ImageView) view.findViewById(R.id.search_pinglun);
            holder.pinglun1 = (ImageView) view.findViewById(R.id.search_pinglun1);
            holder.pinglun2 = (ImageView) view.findViewById(R.id.search_pinglun2);
            holder.pinglun3 = (ImageView) view.findViewById(R.id.search_pinglun3);
            holder.pinglun4 = (ImageView) view.findViewById(R.id.search_pinglun4);
            holder.contentprice = (TextView) view.findViewById(R.id.search_contentprice);
            view.setTag(holder);
        }else {
            view=convertView;
            holder= (ViewHolder) view.getTag();
        }
        holder.listdistance.setText(shopList.get(position).getDistance()+" 公里");
        holder.contenttext.setText(shopList.get(position).getIntro());
        holder.listname.setText(shopList.get(position).getName());
        holder.contentprice.setText(shopList.get(position).getAmount()+" 元/单");
        holder.listpeople.setText(shopList.get(position).getVolume()+" 人付款");
        Double score = shopList.get(position).getScore();
        if (score==0.0){

        }else {
           if (score<1.5&&score>0){
               holder.pinglun.setVisibility(View.INVISIBLE);
               holder.pinglun1.setVisibility(View.GONE);
               holder.pinglun2.setVisibility(View.GONE);
               holder.pinglun3.setVisibility(View.GONE);
           }else if (score>1.5&&score<2.5){
               holder.pinglun.setVisibility(View.INVISIBLE);
               holder.pinglun1.setVisibility(View.GONE);
               holder.pinglun2.setVisibility(View.GONE);
           }else if (score<3.5&&score>2.5){
               holder.pinglun.setVisibility(View.INVISIBLE);
               holder.pinglun1.setVisibility(View.GONE);
           }else if (score<4.5&&score>3.5){
               holder.pinglun.setVisibility(View.INVISIBLE);
           }else {}
        }
        Double distance = shopList.get(position).getDistance();
       if (distance==null){
           holder.listdistance.setText("0"+" 公里");
       }else {
           holder.listdistance.setText(distance+" 公里");
       }


        x.image().bind(holder.head, InternetAddressUtis.IMG_URL+shopList.get(position).getServiceAvatar());
        notifyDataSetChanged();
        return view;
    }

    class ViewHolder{
        ImageView pinglun,pinglun1,pinglun2,pinglun3,pinglun4,head;
        TextView listname,contentprice,contenttext,listdistance,listpeople;
    }
}
